# CCNCore::Database::Local.pm
# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

# This file is part of the CarrierClass.net project.
# For more information, consult http://carrierclass.net/

package CCNCore::Database::Local;
use CCN::Database;
use base qw(CCN::Database);

=pod

    Usage: Provides access to a local database, assuming we are the primary server on a cluster.

    my $local_db = new CCNCore::Provisioning::Local;
    my $into_db  = new CCNCore::Database::Local($local_db,$target); # target is a cluster.pk

    if( $into_db->is_connected )
    {
        $local_db->populate_into($into_db,$sql_file);
    }

=cut

sub initialize
{
    my $self = shift;
    my $local = shift;
    my $target = shift;

    $self->die ('Invalid parameters') unless defined $self and defined $local and defined $target;

    # The process should be as follows:
    # - ensure we are the primary on the db_cluster
    return undef unless $local->system_pk == $local->primary_db_server();

    # - compute access tokens (IP, login, password)
    my $db_cluster = $local->{db_member};
    return unless defined $db_cluster;

    my ($db_name) = $local->sql_get(q(SELECT db_name FROM provisioning WHERE db_cluster = ?),$db_cluster);
    return undef unless defined $db_name;

    # XXX REDO this -- this assumes Debian's security model (root can access any database)
    # which breaks on other OSes (eg CentOS).
    my $dbi_info = "DBI:mysql:database=${db_name}";
    my $login = '';
    my $password = '';
    
    # - connect to the database
    $self->SUPER::initialize($dbi_info,$login,$password);
}

1;